package com.zm.campus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

//redis配置类。最主要是用来实现序列化
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());//实现key的序列化
        //其实redis默认是采用jdk的序列化，jdk不需要传obj，而json2json需要，jdk返回的是字符串，json2json返回的是json对象
        //而GenericJackson2JsonRedisSerializer序列化完也是一个json数据，但是不需要传obj类对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//实现value的序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());//hash的key的序列化
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //注入连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
